package com.wangyk.rabbitmqconsole.controller;

import com.wangyk.rabbitmqcommon.interceptor.Pagination;
import com.wangyk.rabbitmqcommon.model.console.User;
import com.wangyk.rabbitmqcommon.resp.CommonResp;
import com.wangyk.rabbitmqcommon.resp.CommonRespCode;
import com.wangyk.rabbitmqconsole.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description:
 * @Author: wangyk
 * @CreateDate: 2018/11/21$ 17:23$
 * @UpdateUser: wangyk
 * @UpdateDate: 2018/11/21$ 17:23$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Controller
@RequestMapping("/user")
//解决跨域问题
@CrossOrigin(origins = "*", maxAge = 3600)
public class UserController
{
    private Logger logger=LogManager.getLogger(UserController.class);

    @Autowired
    private HttpServletRequest request;

    @Resource
    private UserService userService;

    @RequestMapping("/select_user_by_id.do")
    @ResponseBody
    public CommonResp selectUserById()
    {
        CommonResp resp=new CommonResp();
        User user=userService.selectUserById(1);
        System.out.println(user.getUserName());
        resp.setResult(user);
        resp.setMessage(CommonRespCode.SUCC_MSG);
        resp.setMessage(CommonRespCode.SUCC_CODE);
        return resp;
    }

    @RequestMapping("/select_all.do")
    @ResponseBody
    public CommonResp selectAll()
    {
        String url = request.getParameter("url");
        String name=request.getParameter("name");
        logger.info(url);
        logger.info(name);
        CommonResp resp=new CommonResp();
        List<User> users=userService.selectAll();
        logger.trace("trace");
        logger.debug("debug");
        logger.info("info");
        logger.error("error");
        resp.setResult(users);
        resp.setMessage(CommonRespCode.SUCC_MSG);
        resp.setMessage(CommonRespCode.SUCC_CODE);
        return resp;
    }

    @RequestMapping("/delete_by_primary_key.do")
    @ResponseBody
    public CommonResp deleteByPrimaryKey()
    {
        CommonResp resp=new CommonResp();
        userService.deleteByPrimaryKey(11);
        resp.setResult(null);
        resp.setMessage(CommonRespCode.SUCC_MSG);
        resp.setMessage(CommonRespCode.SUCC_CODE);
        return resp;
    }

    @RequestMapping("/insert_user_return_id.do")
    @ResponseBody
    public CommonResp insertUserReturnId(User user)
    {
        CommonResp resp=new CommonResp();
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        user.setUserName(userName);
        user.setPassword(password);
        System.out.println(user);
        userService.insertUserReturnId(user);
        resp.setResult(user);
        resp.setMessage(CommonRespCode.SUCC_MSG);
        resp.setMessage(CommonRespCode.SUCC_CODE);
        return resp;
    }

    @RequestMapping("/update_user_info.do")
    @ResponseBody
    public CommonResp updateUserInfo()
    {
        CommonResp resp=new CommonResp();
        User user=new User();
        userService.updateUserInfo(user);
        resp.setResult(null);
        resp.setMessage(CommonRespCode.SUCC_MSG);
        resp.setMessage(CommonRespCode.SUCC_CODE);
        return resp;
    }

    @RequestMapping("/select_users_by_page.do")
    @ResponseBody
    public CommonResp selectUserByPage()
    {
        CommonResp resp=new CommonResp();
        resp=userService.selectUsersByPage();
        resp.setMessage(CommonRespCode.SUCC_MSG);
        resp.setMessage(CommonRespCode.SUCC_CODE);
        return resp;
    }

    @RequestMapping("/hot.do")
    @ResponseBody
    public String hotDeploy()
    {
        return "热部署成功！!";
    }
}
